<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TB_TAG 500错误诊断工具</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }
        .container { max-width: 1200px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .header { text-align: center; color: #333; margin-bottom: 30px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
        .test-section h3 { color: #2c3e50; margin-top: 0; }
        .btn { padding: 10px 20px; margin: 5px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; }
        .btn-primary { background-color: #007bff; color: white; }
        .btn-success { background-color: #28a745; color: white; }
        .btn-warning { background-color: #ffc107; color: black; }
        .btn-danger { background-color: #dc3545; color: white; }
        .btn:hover { opacity: 0.8; }
        .result { margin-top: 10px; padding: 10px; border-radius: 4px; white-space: pre-wrap; font-family: monospace; }
        .success { background-color: #d4edda; border: 1px solid #c3e6cb; color: #155724; }
        .error { background-color: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; }
        .info { background-color: #d1ecf1; border: 1px solid #bee5eb; color: #0c5460; }
        .loading { color: #6c757d; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🔧 TB_TAG 500错误诊断工具</h1>
            <p>用于诊断和修复TB_TAG功能的500错误问题</p>
        </div>

        <div class="test-section">
            <h3>📡 基础连接测试</h3>
            <button class="btn btn-primary" onclick="testPing()">测试连接</button>
            <button class="btn btn-warning" onclick="testDiagnose()">系统诊断</button>
            <div id="pingResult" class="result" style="display:none;"></div>
        </div>

        <div class="test-section">
            <h3>📊 数据库测试</h3>
            <button class="btn btn-success" onclick="testList()">查询列表</button>
            <button class="btn btn-primary" onclick="testAdd()">测试新增</button>
            <div id="dbResult" class="result" style="display:none;"></div>
        </div>

        <div class="test-section">
            <h3>🔍 前端API测试</h3>
            <button class="btn btn-primary" onclick="testFrontendAPI()">测试前端API</button>
            <div id="frontendResult" class="result" style="display:none;"></div>
        </div>

        <div class="test-section">
            <h3>🛠️ 快速修复</h3>
            <button class="btn btn-danger" onclick="showFixSteps()">显示修复步骤</button>
            <div id="fixSteps" class="result info" style="display:none;"></div>
        </div>
    </div>

    <script>
        const baseURL = 'http://localhost:8080';
        
        function showResult(elementId, content, type = 'info') {
            const element = document.getElementById(elementId);
            element.className = `result ${type}`;
            element.textContent = content;
            element.style.display = 'block';
        }

        function showLoading(elementId) {
            showResult(elementId, '正在测试...', 'loading');
        }

        async function testPing() {
            showLoading('pingResult');
            try {
                const response = await fetch(`${baseURL}/test/tbtag/ping`);
                const data = await response.json();
                if (response.ok) {
                    showResult('pingResult', `✅ 连接成功!\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showResult('pingResult', `❌ 连接失败!\n状态码: ${response.status}\n响应: ${JSON.stringify(data, null, 2)}`, 'error');
                }
            } catch (error) {
                showResult('pingResult', `❌ 网络错误!\n${error.message}`, 'error');
            }
        }

        async function testDiagnose() {
            showLoading('pingResult');
            try {
                const response = await fetch(`${baseURL}/test/tbtag/diagnose`);
                const data = await response.json();
                if (response.ok) {
                    showResult('pingResult', `🔍 诊断结果:\n${JSON.stringify(data, null, 2)}`, 'info');
                } else {
                    showResult('pingResult', `❌ 诊断失败!\n状态码: ${response.status}\n响应: ${JSON.stringify(data, null, 2)}`, 'error');
                }
            } catch (error) {
                showResult('pingResult', `❌ 诊断错误!\n${error.message}`, 'error');
            }
        }

        async function testList() {
            showLoading('dbResult');
            try {
                const response = await fetch(`${baseURL}/test/tbtag/list`);
                const data = await response.json();
                if (response.ok) {
                    showResult('dbResult', `✅ 查询成功!\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showResult('dbResult', `❌ 查询失败!\n状态码: ${response.status}\n响应: ${JSON.stringify(data, null, 2)}`, 'error');
                }
            } catch (error) {
                showResult('dbResult', `❌ 查询错误!\n${error.message}`, 'error');
            }
        }

        async function testAdd() {
            showLoading('dbResult');
            try {
                const response = await fetch(`${baseURL}/test/tbtag/add`);
                const data = await response.json();
                if (response.ok) {
                    showResult('dbResult', `✅ 新增成功!\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showResult('dbResult', `❌ 新增失败!\n状态码: ${response.status}\n响应: ${JSON.stringify(data, null, 2)}`, 'error');
                }
            } catch (error) {
                showResult('dbResult', `❌ 新增错误!\n${error.message}`, 'error');
            }
        }

        async function testFrontendAPI() {
            showLoading('frontendResult');
            try {
                const response = await fetch(`${baseURL}/homepage/tag/list`);
                const data = await response.json();
                if (response.ok) {
                    showResult('frontendResult', `✅ 前端API成功!\n${JSON.stringify(data, null, 2)}`, 'success');
                } else {
                    showResult('frontendResult', `❌ 前端API失败!\n状态码: ${response.status}\n响应: ${JSON.stringify(data, null, 2)}`, 'error');
                }
            } catch (error) {
                showResult('frontendResult', `❌ 前端API错误!\n${error.message}`, 'error');
            }
        }

        function showFixSteps() {
            const steps = `🔧 修复500错误的步骤:

1. 📊 执行数据库修复脚本:
   mysql -u root -p < quick_fix_500_error.sql

2. 🔄 重新编译项目:
   mvn clean compile

3. 🚀 重启后端服务:
   mvn spring-boot:run -pl ruoyi-admin

4. 🧪 测试API接口:
   - 先测试: http://localhost:8080/test/tbtag/ping
   - 再测试: http://localhost:8080/test/tbtag/diagnose
   - 最后测试: http://localhost:8080/homepage/tag/list

5. 🔍 检查日志:
   查看后端控制台输出的错误信息

6. 📝 常见问题:
   - 表不存在: 执行数据库脚本
   - 权限不足: 检查菜单权限配置
   - Service注入失败: 检查包扫描配置
   - Mapper找不到: 检查MyBatis配置

如果仍有问题，请查看后端日志获取详细错误信息。`;
            
            showResult('fixSteps', steps, 'info');
        }
    </script>
</body>
</html>
